﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace EOrm
{
    /// <summary>
    /// 数据库实体接口
    /// </summary>
    public abstract class Db
    {
        /// <summary>
        /// 数据库名称
        /// </summary>
        public string Name { get; set; } = "";

        /// <summary>
        /// 数据库连接
        /// </summary>
        public IDbConnection DbCon { get; set; } = null;

        /// <summary>
        /// 数据库表集合
        /// </summary>
        public List<ITable> Tables { get; set; } = new List<ITable>();

        /// <summary>
        /// 数据库服务提供者类库的目录
        /// </summary>
        public string ProviderLibDir
        {
            get
            {
                return _providerLibDir;
            }
            set
            {
                if (!value.EndsWith("\\"))
                {
                    _providerLibDir = value + "\\";
                }
                else
                {
                    _providerLibDir = value;
                }
            }
        }
        /// <summary>
        /// 数据库服务提供者类库的目录
        /// </summary>
        private string _providerLibDir = AppDomain.CurrentDomain.BaseDirectory;
        /// <summary>
        /// 数据库服务提供者类库的文件名
        /// </summary>
        public string ProviderLibName
        {
            get; set;
        } = "";

        /// <summary>
        /// 数据库服务提供者的具体路径
        /// </summary>
        public string ProviderLibPath
        {
            get
            {
                if (!File.Exists(ProviderLibDir + ProviderLibName))
                {
                    string existPath = OrmProviderLoader.GetProviderPath();
                    if (existPath == null || string.IsNullOrEmpty(existPath.Trim()))
                    {
                        throw new FileNotFoundException("数据库服务提供者库文件不存在");
                    }
                    ProviderLibDir = Path.GetDirectoryName(existPath);
                    ProviderLibName = Path.GetFileName(existPath);
                }
                if (!File.Exists(ProviderLibDir + ProviderLibName))
                {
                    throw new FileNotFoundException("数据库服务提供者库文件不存在");
                }
                return ProviderLibDir + ProviderLibName;
            }
        }

        /// <summary>
        /// 数据库实例锁
        /// </summary>
        protected static readonly string _dbLock = "dbLock";

        /// <summary>
        /// 数据库单例实体
        /// </summary>
        protected static Db _dbInstance = null;

        /// <summary>
        /// 创建数据库实例
        /// </summary>
        /// <param name="jsonConfigPathOrConString">json配置文件路径或连接字符串</param>
        /// <returns>数据库</returns>
        public static Db CreateInstance(string dbProviderPath, string jsonConfigPathOrConString)
        {
            if (_dbInstance == null)
            {
                if (dbProviderPath == null || string.IsNullOrEmpty(dbProviderPath.Trim()))
                {
                    dbProviderPath = OrmProviderLoader.GetProviderPath();
                }
                if (jsonConfigPathOrConString == null)
                {
                    jsonConfigPathOrConString = "";
                }
                lock (_dbLock)
                {
                    if (_dbInstance== null)
                    {
                        IDbFactory dbFactory = OrmProviderLoader.GetInterface<IDbFactory>(dbProviderPath);
                        _dbInstance = dbFactory.Create(jsonConfigPathOrConString);
                    }
                }
            }
            return _dbInstance;
        }
    }
}
